#pragma once
#include <string.h>
#include <iostream>
#include <assert.h>
using namespace std;

namespace ys
{
	template<class T>
	class vector
	{
	public:
		typedef T* iterator;
		typedef const T* const_iterator;
		iterator begin()
		{
			return _start;
		}
		iterator end()
		{
			return _finish;
		}

		const_iterator begin() const
		{
			return _start;
		}
		const_iterator end() const
		{
			return _finish;
		}

		vector()
			:_start(nullptr)
			,_finish(nullptr)
			,_endofstorage(nullptr)
		{}

		vector(const vector<T>& v)
			:_start(nullptr)
			, _finish(nullptr)
			, _endofstorage(nullptr)
		{
			reserve(v.capacity());
			for (auto& e : v)
			{
				push_back(e);
			}
		}

		template <class InputIterator>
		vector(InputIterator first, InputIterator last)
		{}
		vector(size_t n, const T val = T())
		{}
		vector(int n, const T val = T())
		{}

		void swap(vector<T> tmp)
		{
			std;; swap(_start, tmp._start);
			std;; swap(_finish, tmp._finish);
			std:: swap(_endofstorage, tmp._endofstorage);
		}

		vector& operator=(vector<T> tmp)
		{
			swap(tmp);
			return *this;
		}

		~vector()
		{
			delete[] _start;
			_start = _finish = _endofstorage = nullptr;
		}

		void push_back(const T& x)
		{
			if (_finish == _endofstorage)
			{
				reserve(capacity() == 0 ? 4 : capacity() * 2);
			}
			*_finish = x;
			_finish++;
		}

		void reserve(size_t cp)
		{
			if (cp > capacity())
			{
				size_t sz = size();
				T* tmp = new T[cp];
				if (_start)
				{
					//memcpy(tmp, _start, sizeof(T) * sz);
					for (int i = 0; i < sz; i++)
					{
						tmp[i] = _start[i];
					}
					delete[] _start;
				}
				_start = tmp;

				_finish = _start + sz;
				_endofstorage = _start + cp;
			}
		}
		//void resize(size_t n, T val = T())
		void resize(size_t n, const T& val = T())
		{
			if (n < size())
			{
				_finish = _start + n;
			}
			else
			{
				reserve(n);
				while (_finish < _start + n)
				{
					*_finish = val;
					_finish++;
				}
			}
		}

		void insert(iterator pos, const T& x)
		{
			assert(pos >= _start && pos <= _finish);
			if (_finish == _endofstorage)
			{
				size_t len = pos - _start;
				reserve(capacity() == 0 ? 4 : capacity() * 2);
				pos = _start + len;
			}

			iterator end = _finish - 1;
			while (end >= pos)
			{
				*(end + 1) = *end;
				end--;
			}
			*pos = x;
			++_finish;
		}

		iterator erase(iterator pos)
		{
			assert(pos >= _start && pos < _finish);
			iterator it = pos + 1;
			while (it < _finish)
			{
				*(it - 1) = it;
				it++;
			}
			--_finish;
			return pos;
		}

		T& operator[](size_t pos)
		{
			assert(pos < size());
			return _start[pos];
		}

		const T& operator[](size_t pos) const
		{
			assert(pos < size());
			return _start[pos];
		}

		size_t capacity() const
		{
			return _endofstorage - _start;
		}

		size_t size() const
		{
			return _finish - _start;
		}
	private:
		iterator _start;
		iterator _finish;
		iterator _endofstorage;
	};

	void test1()
	{
		vector<int> v;
		v.push_back(1);
		v.push_back(2);
		v.push_back(3);
		v.push_back(4);
		v.push_back(5);

		vector<int>::iterator it = v.begin();
		while (it != v.end())
		{
			cout << *it << " ";
			it++;
		}
		cout << endl;

		for (auto e : v)
		{
			cout << e << " ";
		}

	}

	void test2()
	{
		vector<int> v1;
		v1.resize(10);

		vector<string> v2;
		v2.resize(5, "xx");

		for (auto e : v1)
		{
			cout << e << " ";
		}
		cout << endl;

		for (auto s : v2)
		{
			cout << s << " ";
		}
		cout << endl;
	}
	void test3()
	{
		vector<int> v;
		vector<int>::iterator it = v.begin();
		v.insert(it, 20);

	
	}

	void test4()
	{
		vector<string> s;
		s.push_back("1111111");
		s.push_back("1111111");
		s.push_back("1111111");
		s.push_back("1111111");
		s.push_back("1111111");

		for (auto e : s)
		{
			cout << e << " ";
		}
	}

	void test5()
	{
		vector<int> v(10, 0);

		
	}
}